package com.ohy.tencent.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ohy.tencent.config.Contan;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("iqiyi")
@Slf4j
public class IqiyiVideoController {

    @GetMapping(value = "getClass")
    public String getMVClass(@RequestParam(value = "type", defaultValue = "1") Integer type) {
        String url = "https://list.iqiyi.com/www/" + type + "/-------------14-1-1-iqiyi--.html";
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        Document document = Jsoup.parse(execute.body());
        Element element = document.getElementById("block-B");
        JSONArray jsonArray = JSONArray.parseArray(element.attr(":first-category-list"));
        JSONObject order = new JSONObject();
        order.put("name", "排序");
        order.put("child", JSONArray.parse(element.attr(":order-list")));
        jsonArray.add(order);
        JSONObject pay = new JSONObject();
        pay.put("name", "资费");
        pay.put("child", JSONObject.parseObject(element.attr(":pay-status")).getJSONArray("list"));
        jsonArray.add(pay);
        JSONObject year = new JSONObject();
        year.put("name", "年份");
        year.put("child", JSONObject.parseObject(element.attr(":my-year")).getJSONArray("list"));
        jsonArray.add(year);
        return jsonArray.toString();
    }

    @GetMapping(value = "getVideo")
    public String getVideo(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                           @RequestParam(value = "资费", required = false, defaultValue = "") String purchase,
                           @RequestParam(value = "年份", required = false, defaultValue = "") String date,
                           @RequestParam(value = "排序", required = false, defaultValue = "11") String mode,
                           @RequestParam(value = "地区", required = false, defaultValue = "") String iarea,
                           @RequestParam(value = "类型", required = false, defaultValue = "") String type,
                           @RequestParam(value = "isTV", required = false, defaultValue = "1") int isTV,
                           @RequestParam(value = "规格", required = false, defaultValue = "") String specification) {
        String three_category_id = "";
        if (!StringUtils.isEmpty(iarea)) three_category_id += iarea + ";must,";
        if (!StringUtils.isEmpty(type)) three_category_id += type + ";must,";
        if (!StringUtils.isEmpty(specification)) three_category_id += specification + ";must,";
        String url = "https://pcw-api.iqiyi.com/search/video/videolists?access_play_control_platform=14" +
                "&channel_id=" + (isTV == 1 ? 1 : 2) +
                "&data_type=1&without_qipu=1&from=pcw_list&is_album_finished=" +
                "&is_purchase=" + purchase +//资费
                "&key=&market_release_date_level=" + date +
                "&mode=" + mode +//排序
                "&pageNum=" + page + "&pageSize=48" +
                "&site=iqiyi&source_type=" +
                "&three_category_id=" + three_category_id;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getTVInfo")
    public String getTVInfo(@RequestParam(value = "aid") String aid, @RequestParam(value = "size", required = false, defaultValue = "100") int size) {
        String url = "https://pcw-api.iqiyi.com/albums/album/avlistinfo?aid=" + aid + "&page=1&size=" + size;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }

    @GetMapping(value = "getSearch")
    public String getSearch(@RequestParam(value = "word") String word) throws UnsupportedEncodingException {
        String url = "https://suggest.video.iqiyi.com/?key" + URLEncoder.encode(word, "UTF-8");
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        return execute.body();
    }


    @GetMapping(value = "getSearchRes")
    public String getSearchRes(@RequestParam(value = "word") String word) {
        String url = "https://so.iqiyi.com/so/q_" + word;
        HttpRequest get = HttpUtil.createGet(url);
        HttpResponse execute = get.execute();
        String res = execute.body();
        Document parse = Jsoup.parse(res);
        Elements elements = parse.getElementsByClass("vertical-pic");
        List<JSONObject> list = new ArrayList<>();
        for (Element element : elements) {
            try {
                String b = "";
                Elements em = element.getElementsByTag("em");
                for (Element e : em) {
                    if (e.text().equals("爱奇艺")) {
                        b = "yes";
                        break;
                    }
                }
                if (StringUtils.isEmpty(b)) continue;
                //这是电影
                if (element.getElementsByClass("score").size() > 0 && element.getElementsByClass("item-type").contains("电影")) {
                    String score = element.getElementsByClass("score").get(0).text();
                    String title = element.getElementsByClass("qy-mod-cover").get(0).attr("title");
                    url = element.getElementsByClass("result-figure").get(0).getElementsByTag("a").get(0).attr("href");
                    String pic = element.getElementsByClass("result-figure").get(0).getElementsByTag("img").get(0).attr("src");
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("score", score);
                    jsonObject.put("title", title);
                    jsonObject.put("pic", pic);
                    jsonObject.put("url", url);
                    jsonObject.put("type", "mv");
                    list.add(jsonObject);
                } else if (element.getElementsByAttribute("data-widget-searchlist-albumid").size() > 0
                        && element.getElementsByClass("album-list").size() > 0
                        && element.getElementsByAttribute("data-widget-searchlist-albumid").get(0).attr("data-widget-searchlist-albumid").length() == "204218901".length()) {
                    String aid = element.getElementsByAttribute("data-widget-searchlist-albumid").get(0).attr("data-widget-searchlist-albumid");
                    String title = element.getElementsByAttribute("data-widget-searchlist-albumid").get(0).attr("data-searchpingback-albumname");
                    String pic = element.getElementsByClass("result-figure").get(0).getElementsByTag("img").get(0).attr("src");
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("aid", aid);
                    jsonObject.put("title", title);
                    jsonObject.put("pic", pic);
                    jsonObject.put("type", "tv");
                    list.add(jsonObject);
                } else if (element.toString().contains("立即播放")) {
                    String title = element.getElementsByClass("qy-mod-cover").get(0).attr("title");
                    url = element.getElementsByClass("result-figure").get(0).getElementsByTag("a").get(0).attr("href");
                    String pic = element.getElementsByClass("result-figure").get(0).getElementsByTag("img").get(0).attr("src");
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("title", title);
                    jsonObject.put("pic", pic);
                    jsonObject.put("url", url);
                    jsonObject.put("type", "mv");
                    list.add(jsonObject);
                }
            } catch (Exception e) {
                log.error("爱奇艺解析搜索异常", e);
            }
        }
        return list.toString();
    }
}
